package linkedlist;

public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(21, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(44, "林冲", "豹子头");
        HeroNode2 hero5 = new HeroNode2(29, "AAA", "aaa");
        HeroNode2 hero6 = new HeroNode2(21, "BBB", "bbb");
        HeroNode2 hero7 = new HeroNode2(79, "CCC", "ccc");
        HeroNode2 hero8 = new HeroNode2(28, "DDD", "ddd");
        DoubleLinkedList singleLinkedList = new DoubleLinkedList();
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero3);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero5);
        singleLinkedList.addByOrder(hero6);
        singleLinkedList.addByOrder(hero7);
        singleLinkedList.addByOrder(hero8);
        singleLinkedList.list();
        singleLinkedList.delete(3);
        singleLinkedList.list();

    }
}


//创建一个双向链表类
class DoubleLinkedList{

    private HeroNode2 head = new HeroNode2(0,"","");//头节点

    public HeroNode2 getHead(){
        return head;
    }

    public boolean isNull(){
        return head.next == null;
    }

    public int size(){
        int size = 0;
        if (isNull()){
            return 0;
        }
        HeroNode2 temp = head.next;
        while (true){
            if (temp == null){
                break;
            }
            size ++;
            temp = temp.next;
        }
        return size;
    }



    //遍历双向链表
    public void list(){
        //如果链表的next一开始为空，说明这是空链表
        if (isNull()){
            System.out.println("链表为空");
            //直接结束方法
            return;
        }
        //执行到这里意味着头节点以下是存在其他节点的，后续将通过temp简介操作链表
        HeroNode2 temp = head.next;

        while (true){//设置死循环
            if (temp == null) break;//如果当前节点为null，就证明链表遍历完成
            System.out.println(temp);
            temp = temp.next;
        }

    }

    //添加到双向链表的最后
    public void add(HeroNode2 heroNode){
        HeroNode2 temp = head;
        while (true){
            if (temp.next == null){//当temp的next为null时，表示当前遍历到链表最后一个
                temp.next = heroNode;//将temp.next指向新元素
                heroNode.pre = temp;//新元素的pre指向temp
                break;
            }
            temp = temp.next;//若if不成立，说明temp还有下一个节点，将使用下一个节点继续循环
        }
    }

    public void addByOrder(HeroNode2 heroNode){

        HeroNode2 temp = head;
        boolean flag = false;//标志添加的符号是否存在，默认为false
        while (true){
            if (temp.next==null){//说明在链表最后
                break;//
            }
            if (temp.next.no > heroNode.no){//位置找到，就在temp的后面插入
                break;
            }else if(temp.next.no == heroNode.no){//序号已存在
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //判断flag的值
        if (flag){
            System.out.printf("编号已存在，不能加入%s",heroNode.no);
        }else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    //修改一个结点的内容
    public void update(HeroNode2 newHero){
        if (isNull()){
            System.out.println("列表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if (temp == null){
                System.out.println("没有找到要更新的节点");
            }
            if (temp.no == newHero.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            newHero.next = temp.next;
            newHero.pre = temp.pre;
        }
    }

    //删除双向链表的节点
    public void delete(int no){
        if (isNull()){
            System.out.println("空列表");
            return;
        }
        HeroNode2 temp = head.next;
        while (true){
            if (temp == null){
                System.out.println("找不到需要删除的节点");
                break;
            }
            if (temp.no == no){
                temp.pre.next = temp.next;
                //此处有问题，如果删除最后一个节点，会导致temp.next.pre可能为null报错，也就是空指针异常
//                temp.next.pre = temp.pre;
                if (temp.next != null) temp.next.pre = temp.pre;
                break;
            }
            temp = temp.next;
        }
    }
}

class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;//指向下一个节点
    public HeroNode2 pre;//指向下一个节点

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
//                ", next=" + next +
//                ", pre=" + pre +
                '}';
    }

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
}